package ged.customizationCode.functions;

import ged.Node;

import java.awt.GraphicsEnvironment;
import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.draw2d.ui.mapmode.IMapMode;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Display;
import org.osgi.framework.Bundle;

public class CustomizationCodeNodeEditPart {

	//Node node;
	
	public CustomizationCodeNodeEditPart()
	{
		
	}
	
	//set Font Color
	public void setFontColor(WrappingLabel fFigureNodeLabelFigure, Node node) {
		ged.Color fontColor = node.getFontColor();
		if(fontColor != null)
		{
			fFigureNodeLabelFigure.setForegroundColor(new Color(Display.getCurrent(),
					fontColor.getRed(), fontColor.getGreen(), fontColor
					.getBlue()));
		}
	}
	
	//set font of the label - bold - italic - underline - strikeThrough - fontName - fontHeight
	public void setLabelFont(WrappingLabel fFigureNodeLabelFigure, Node node) {
		ged.Font font = node.getFont();
		if (font != null) {
			//StrikeThrough 
			fFigureNodeLabelFigure.setTextStrikeThrough(font
					.isLabelStrikeThrough());
			// underline
			fFigureNodeLabelFigure
					.setTextUnderline(font.isLabelUnderline());
			// ged.fontName fontName = node.getLabel_fontName();
			String fontName = font.getFname();	    
		    
			//if the given font does not, exist, set the default font "Tahoma"
			if(fontName == null)
				fontName = "";
		    if(!validFontName(fontName))
		    {
		    	System.out.println("The font does not exist");
	    		fontName = "Tahoma";
		    }

		    System.out.println("The font to apply is: " + fontName);
		    
			int height = font.getFontHeight();
			// change Font
			// BOLD and ITALIC
			if (font.isLabelBold() && font.isLabelItalic()) {
				fFigureNodeLabelFigure.setFont(
						new Font(Display.getCurrent(), fontName,
								(height != 0 ? height : 9), SWT.BOLD
										+ SWT.ITALIC));
			}
			// ITALIC
			else if (font.isLabelItalic()) {
				fFigureNodeLabelFigure.setFont(
						new Font(Display.getCurrent(), fontName,
								(height != 0 ? height : 9), SWT.ITALIC));
			}
			// BOLD
			else if (font.isLabelBold()) {
				fFigureNodeLabelFigure.setFont(
						new Font(Display.getCurrent(), fontName,
								(height != 0 ? height : 9), SWT.BOLD));
			} else {
				fFigureNodeLabelFigure.setFont(
						new Font(Display.getCurrent(), fontName,
								(height != 0 ? height : 9), SWT.NORMAL));
			}
		}
	}
	
	/**check if the given font is valid
	 * 
	 * @param thefontName
	 * @return
	 */
	public boolean validFontName(String thefontName)
	{
		//get all available fonts in the system
		GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
	    java.awt.Font[] fonts = e.getAllFonts(); // Get the fonts
	    /*for (java.awt.Font f : fonts) {
	      System.out.println(f.getFontName());
	    }*/
	    
	  //compare the value of the attribute 
	    for(java.awt.Font f : fonts)
	    {
	    	String fontToString = f.getFontName();
	    	//if the given font exists in the available fonts
	    	if(fontToString.equals(null))
	    		return false;
	    	else if(thefontName.equals(fontToString))
		    	return true;
	    }
		return false;		
	}
	
	//set background colour
	public void setBackGround_Color(org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure figure, Node node)
	{
		ged.Color backColor = node.getBackgroundColor();
		if (backColor != null)
			figure.setBackgroundColor(new Color(Display.getCurrent(), backColor
					.getRed(), backColor.getGreen(), backColor.getBlue()));
	}
	
	//set foreground colour
	public void setForeGround_Color(org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure figure, Node node)
	{
		ged.Color foregrColor = node.getForegroundColor();
		if (foregrColor != null)
			figure.setForegroundColor(new Color(Display.getCurrent(),
					foregrColor.getRed(), foregrColor.getGreen(), foregrColor
							.getBlue()));
	}
	
	//sets the margin for the figure. The nodeFigure is placed inner in the figure. - useless
	public void setOuterMargin(org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure figure, IMapMode mapMode, Node node)
	{
		ged.BorderMargin borderMargin = node.getBorderMargin();
		if (borderMargin != null)
			figure.setBorder(new MarginBorder(mapMode.DPtoLP(
					borderMargin.getMargin_top()), mapMode.DPtoLP(
					borderMargin.getMargin_left()), mapMode.DPtoLP(
					borderMargin.getMargin_bottom()), mapMode.DPtoLP(
					borderMargin.getMargin_right())));
	}
	
	public int setPositionOfLabel(ged.Label_attributes label_attributes)
	{
		int preferredSide = 0;
		if(label_attributes != null)
		{
			ged.LabelPosition label_position = label_attributes.getLabel_position();
			switch(label_position){
			case BOTTOM:
				preferredSide = PositionConstants.SOUTH; // preferredSide = PositionConstants.CENTER + PositionConstants.BOTTOM;
				break;
			case BOTTOM_LEFT:
				preferredSide = PositionConstants.SOUTH_WEST;
				break;
			case BOTTOM_RIGHT:
				preferredSide = PositionConstants.SOUTH;//bottom right
				break;
			case TOP:
				preferredSide = PositionConstants.CENTER + PositionConstants.TOP;
				break;
			case TOP_LEFT:
				preferredSide = PositionConstants.NORTH_WEST;//top left
				break;
			/*case TOP_RIGHT:
				preferredSide = PositionConstants.NORTH;//top right
				break;*/
			case LEFT:
				preferredSide = PositionConstants.WEST;//left
				break;
			case RIGHT:
				preferredSide = PositionConstants.EAST;//right
				break;
			case CENTER:
				preferredSide = PositionConstants.CENTER;
				break;
			default:
				preferredSide = PositionConstants.NORTH;//top right -- default is the top right location
				break;	
			}
		}
		return preferredSide;
	}
	
	public URL setSelectedImage(ged.NodeImage theImage, Bundle bundle) 
	{
		URL newImage = null;
		switch(theImage) {
		case ACTOR:
			newImage = FileLocator.find(bundle, new Path("icons/nodes/node_actor.png"), null);
			break;
		case DATABASE:
			newImage = FileLocator.find(bundle, new Path("icons/nodes/node_database.png"), null);
			break;
		default:
			newImage = null;
			break;
		}
		return newImage;
	}
	
	//set the type of the line of the border
	public int getCurrentBorderLineType(Node node)
	{
		ged.LineType lineType = node.getBorder_lineType();
		int type = 0;
		switch (lineType) {
		case DASH:
			type = SWT.LINE_DASH;
			break;
		case DASH_DOT:
			type = SWT.LINE_DASHDOT;
			break;
		case DASH_DOT_DOT:
			type = SWT.LINE_DASHDOTDOT;
			break;
		case DOT:
			type = SWT.LINE_DOT;
			break;
		default:
			type = SWT.LINE_SOLID;
			break;
		}
		return type;
	}
	
	public void setToolTip(org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure figure, Node node)
	{
		String toolTipText;			
		if (node.getToolTip() != null && node.getToolTip().length() > 0) {
				toolTipText = node.getToolTip();
				if(figure.getToolTip() == null)
				{
					figure.setToolTip(new Label(toolTipText));
				}
				else if (figure.getToolTip() instanceof Label)
				{
					((Label) figure.getToolTip()).setText(toolTipText);
				}
		}
	}
	
	//set the attributes of the label
/*	public void setLabeAttributes(WrappingLabel fFigureNodeLabelFigure, Node node)
	{
		ged.Label_attributes l_attributes = node.getLabel_attributes();
		if(l_attributes != null)
		{
			if(!l_attributes.isIcon())
				fFigureNodeLabelFigure.setIcon(null);
				
			ged.LabelPlacement placement = l_attributes.getLabel_placement();
			switch(placement){
			case NONE:
				fFigureNodeLabelFigure.setTextPlacement(PositionConstants.NONE);
				break;
			case EXTERNAL:
				fFigureNodeLabelFigure.setTextPlacement(PositionConstants.NORTH);
				break;
			case INTERNAL:
				fFigureNodeLabelFigure.setTextPlacement(PositionConstants.EAST);
				break;
			default:
				fFigureNodeLabelFigure.setTextPlacement(PositionConstants.EAST);	
				break;
			}
			
		}
	}*/

}
